package com.stu.boke.controller.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.stu.boke.entity.Menu;
import com.stu.boke.service.MenuService;

@Controller
@RequestMapping(value = "/admin/menu")
public class MenuController {

	@Autowired
	private MenuService menuService;
	
	@RequestMapping("")
	public ModelAndView indexMenu() {
		ModelAndView model = new ModelAndView();
		List<Menu> menuList = menuService.listMenu();
		model.addObject("menuList", menuList);
		model.setViewName("Admin/Menu/index");
		return model;
	}
	
	@RequestMapping(value = "/insertSubmit")
	public String insertSubmitMenu(Menu menu) {
		if(null == menu.getMenuOrder()) {
			menu.setMenuOrder(1);
		}
		menuService.insertSubmitMenu(menu);
		return "redirect:/admin/menu";
	}
	
	@RequestMapping(value = "/edit/{id}")
	public ModelAndView editMenuView(@PathVariable("id") Integer id) {
		ModelAndView modelAndView = new ModelAndView();
		Menu menu = menuService.getMenuById(id);
		modelAndView.addObject("menu", menu);
		modelAndView.setViewName("Admin/Menu/edit");
		return modelAndView;
	}
	
	@RequestMapping(value = "/editSubmit")
	public String editMenuSubmit(Menu menu) {
		menuService.updateMenu(menu);
		return "redirect:/admin/menu";
	}
	
	@RequestMapping(value = "/insert")
	public ModelAndView insertArticle(Model model) {
		ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("Admin/Menu/insert");
        return modelAndView;
	}
	
	@RequestMapping(value = "/delete/{id}")
	public String deleteMenu(@PathVariable("id") Integer id) {
		menuService.deleteMenu(id);
		return "redirect:/admin/menu";
	}
}
